<?php

function param_get($name, $type = "string", $default = null){
    return param_from_array($_GET, $name, $type, $default);
}

function param_post($name, $type = "string", $default = null){
    return param_from_array($_POST, $name, $type, $default);
}

function param_cookie($name, $type = "string", $default = null){
    return param_from_array($_COOKIE, $name, $type, $default);
}

function param_server($name, $type = "string", $default = null){
    return param_from_array($_SERVER, $name, $type, $default);
}

function param_from_array($array, $name, $type, $default = null){
    if(!isset($array[$name])){
        return $default;
    } else {
        return _param_conv_type($array[$name], $type);
    }
}


function _param_conv_type($value, $type){
    $retval = null;

    switch($type){

    case "int":
    case "integer":
        $retval = intval($value);    
        break;

    case "bool":
    case "boolean":
        if(strtolower($value) == 'true'){
            $retval = true;
        } else {
            $retval = false;
        }
        break;

    case "str":
    case "string":
        $retval = trim($value);
        break;

    default:
        $retval = $value;

    }

    return $retval;
}

